/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carrello;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;

/**
 *
 * @author v
 */
@Stateful
public class CartBean implements Cart {
    List<String> contents;
    String customerId;
    String customerName;

    public void initialize(String person) throws Exception {
        if (person == null) 
            throw  new Exception("null person not allowed");
        else
            customerId = person;
        
        customerId = "0";
        contents = new ArrayList<String>();
    }

    public void initialize(String person, String id) throws Exception {
        if (person == null)
            throw  new Exception("null person not allowed");
        else
            customerId = person;
        IdVerifier idChecker = new IdVerifier();

        if (idChecker.validate(id))
            customerId = id;
        else
            throw  new Exception("Invalid id " + id);

        contents = new ArrayList<String>();
    }

    public void addBook(String title) {
        contents.add(title);
    }

    public void removeBook(String title) throws Exception {
        if (contents.isEmpty() || !contents.contains(title))
            throw  new Exception("null person not allowed");
        else
            contents.remove(title);
    }

    public Collection<String> getContents() {
        return contents;
    }

    @Remove
    public void remove() {
        contents = null;
        System.gc();
    }
    
   
}
